local redis = require 'redis'
local settings = {
    host = '47.113.124.224';
    port = '6379';
    timeout = 300;
}
local client = redis.connect(settings)
--client:auth()
-- ["rl_7f174ded_722a3985", "rlts_7f174ded_722a3985", "1.0", "1", "1727430906", +1 more]
client:select(0)
client:set('lsz', 'bar')
local value = client:get('lsz')

local tokens_key = 'rl_7f174ded_722a3985'
local timestamp_key = 'rlts_7f174ded_722a3985'
local rate = tonumber(1.0)
local capacity = tonumber(1)
local now = tonumber(1727430906)
local requested = tonumber(1)
local fill_time = capacity / rate
local ttl = math.floor(fill_time * 2)
local last_tokens = tonumber(client:get(tokens_key))
if last_tokens == nil then
    last_tokens = capacity
end
local last_refreshed = tonumber(client:get(timestamp_key))
if last_refreshed == nil then
    last_refreshed = 0
end
local delta = math.max(0, now - last_refreshed)
local filled_tokens = math.min(capacity, last_tokens + (delta * rate))
local allowed = filled_tokens >= requested
local new_tokens = filled_tokens
if allowed then
    new_tokens = filled_tokens - requested
end
client:setex(tokens_key, ttl, new_tokens)
client:setex(timestamp_key, ttl, now)
print( allowed)